package com.drive.controller;

import com.drive.bean.Order;
import com.drive.constant.MessageConstant;
import com.drive.constant.RedisMessageConstant;
import com.drive.entity.Result;
import com.drive.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Map;

/**
 * @author 往
 * 前天模块
 * 科目预约
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private JedisPool jedisPool;

    //处理预约请求
    @RequestMapping("/submit")
    public Result submit(@RequestBody Map map){
        //获取map集合中的手机号
        String telephone = (String) map.get("telephone");
        //从redis中获取缓存的验证码
        String validateCodeInRedis = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_ORDER);
        //从map集合中获取验证码
        String validateCodeInPage = (String) map.get("validateCode");
        //校验验证码是否匹配
        if (validateCodeInRedis==null || !validateCodeInRedis.equals(validateCodeInPage)){
                return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }
        Result result = null;
        //调用体检预约服务
        try{
            map.put("orderType", Order.ORDERTYPE_WEIXIN);
            result = orderService.order(map);
            return result;
        }catch (Exception e){
            return new Result(false,MessageConstant.ORDER_FAIL);
        }
    }

    //根据预约id查询信息
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            Map map = orderService.findById(id);
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,map);
        }catch (Exception e){
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }
}
